﻿using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Security.Strategy;
using DevExpress.Persistent.Base;
using DevExpress.Xpo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace SecurityDemo.Module
{
    [NonPersistent]
    [Serializable]
    [System.ComponentModel.DisplayName("Log On")]
    public class SecurityDemoAuthenticationLogonParameters : INotifyPropertyChanged, ISerializable
    {
        private IObjectSpace objectSpace;
        private XPCollection<SecuritySystemUser> availableUsers;
        private SecuritySystemUser user;

        public SecurityDemoAuthenticationLogonParameters() { }
        public SecurityDemoAuthenticationLogonParameters(SerializationInfo info, StreamingContext context)
        {
            if (info.MemberCount > 0)
            {
                UserName = info.GetString("UserName");
            }
        }

        [Browsable(false)]
        public IObjectSpace ObjectSpace
        {
            get { return objectSpace; }
            set { objectSpace = value; }
        }
        [Browsable(false)]
        public XPCollection<SecuritySystemUser> AvailableUsers
        {
            get
            {
                if (availableUsers == null)
                {

                    availableUsers = (XPCollection<SecuritySystemUser>)ObjectSpace.GetObjects<SecuritySystemUser>();
                    availableUsers.BindingBehavior = CollectionBindingBehavior.AllowNone;
                }
                return availableUsers;
            }
        }
        [DataSourceProperty("AvailableUsers"), ImmediatePostData]
        public SecuritySystemUser User
        {
            get { return user; }
            set
            {
                user = value;
                if (user != null)
                {
                    UserName = user.UserName;
                }
                else
                {
                    UserName = "";
                }
                OnPropertyChanged("User");
            }
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("UserName", UserName);
        }
        [Browsable(false)]
        public string UserName { get; set; }
    }

}
